import nedb from './nedb'
export default {
  /**
   * 获取公众号对应的通用回复
   * @param {int} auid 公众号ID
   */
  async getUsual (auid) {
    try {
      var result = await nedb.usual_msg.find({
        auid: auid
      })
      if (result.length > 0) {
        return Promise.resolve(result[0].data)
      }
      return Promise.resolve(null)
    } catch (e) {
      return Promise.resolve(null)
    }
  },
  /**
   * @param {int} auid 公众号ID
   * @param {Array} data 通用回复列表
   */
  async addUsual (auid, data) {
    try {
      var oldResult = await nedb.usual_msg.find({
        auid: auid
      })
      if (oldResult.length > 0) {
        await nedb.usual_msg.update({auid: auid}, {
          auid: auid,
          data: data
        })
      } else {
        await nedb.usual_msg.insert({
          auid: auid,
          data: data
        })
      }
      return Promise.resolve(data)
    } catch (e) {
      return Promise.resolve(null)
    }
  }
}
